<?php

namespace Common\Model;

use Think\Model;

/**
 * 文章模型
 */
class ArticleModel extends Model {

    const NOREVIEW = 0;       // 文档状态：未审核
    const REVIEWED_YES = 1;   // 文档状态：审核通过
    const REVIEWED_NO = -1;   // 文档状态：审核被拒

    const MOREN_CATE = 0;       // 文档写在前面页面的目录ID

    const ARTICLE_KEY = 'art:';     // 文章信息缓存键名

    /**
     * 添加一条文章数据
     * @param $data
     * @return bool
     */
    public static function addData($data){
        return M('Article')->add($data);
    }

    /**
     * 创建一份文档提交
     * @param $data
     * @return bool
     */
    public static function createArticle($data)
    {
        if ($aid = self::addData($data)) {
            DocCountModel::update($data['did'], 'change');
            if ($data['status'] == ArticleModel::REVIEWED_YES) {
                self::clearCurArticleCache($data['did'], $data['cid']);
            } else {
                DocCountModel::update($data['did'], 'review');
            }
            return $aid;
        }
        return false;
    }

    /**
     * 更新文档的文章
     * @param $article
     * @param $data
     * @return bool
     */
    public static function update($article, $data){
        if(M('Article')->where(['id' => $article['id']])->save($data)){
            DocCountModel::update($article['did'], 'review', false);
            if($data['status'] == self::REVIEWED_YES){
                self::clearCurArticleCache($article['did'], $article['cid']);
            }
            return true;
        }
        return false;
    }

    /**
     * 根据文章ID获取文章信息
     * @param $aid
     * @return mixed
     */
    public static function getArticleById($aid){
        return M('Article')->find($aid);
    }

    /**
     * 获取指定文档指定分类对应的文章信息[包括md，html等]
     * @param $did
     * @param $cid
     * @return mixed
     */
    public static function getCurArticle($did, $cid){
        $key = self::ARTICLE_KEY.$did.':'.$cid;     // 如： art:1:12
        $result = S($key);
        if(false === $result){
            $result = M('Article')->where(['did' => $did, 'cid' => $cid, 'status' => self::REVIEWED_YES])
                ->order(['update' => 'desc'])->find();
            if(false !== $result){
                S($key, is_null($result) ? [] : $result);
            }
        }
        return $result;
    }

    /**
     * 分页获取文档编辑提交列表
     * @param $did
     * @param $p
     * @param $pcount
     * @return array
     */
    public static function getArticleList($did, $p, $pcount){
        $count = M('Article')->where(['did' => $did])->count();
        $list = $count ? M()->table(['wo_article' => 'a', 'wo_user' => 'u'])->where('a.did = '.$did.' and a.uid = u.id')->field('a.id,a.cid,a.title,a.desc,a.uid,a.create,a.status,u.nick')->order('a.status = 0 desc, a.create desc')->page($p.','.$pcount)->select() : array();
        return [
            'count' => $count,
            'list' => $list
        ];
    }

    public static function clearCurArticleCache($did, $cid){
        $key = self::ARTICLE_KEY.$did.':'.$cid;
        S($key, null);
    }

}